﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2011
*/
using System;
using System.ComponentModel;
using System.Data;
using NetBay.Core.Log;
using NetBay.Core.DataAccess;
using NetBay.Core.Extensions;
using NetBay.Core.BusinessEntities.Versioning;

namespace Rio.Manager.DataAccess
{
    public class DA_Versioning
    {
        #region Enums
        public enum DbFunction
        {
            [DescriptionAttribute("")]

            None,
            [DescriptionAttribute("P_VERSION_MODEL_Add")]

            AddVersionModel,
            [DescriptionAttribute("P_VERSION_MODEL_Get")]

            GetVersionModel,
            [DescriptionAttribute("P_VERSION_MODEL_Update")]

            UpdateVersionModel,
            [DescriptionAttribute("P_VERSION_MODEL_HasCheckout")]

            HasCheckoutVersion,
            [DescriptionAttribute("P_VERSION_CHECKOUT_Get")]

            GetCheckOutVersion,
            [DescriptionAttribute("P_DOCUMENT_VERS_DeleteAll")]

            DeleteAllDocumentVersion,
        }

        #endregion

        #region Function

        /// <summary>
        /// Execute P_VERSION_MODEL_Add
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static void SetVersionModel(long FolderTypeIdentifier, VersioningModel Model)
        {
            try
            {
                //LogManager.Instance().LogDebug("(p_dStartDate<" + StartDate.ToString() + ">)(p_dEndDate<" + EndDate.ToString() + ">)");
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.AddVersionModel.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int32, FolderTypeIdentifier);
                db.AddInParameter(dbCommand, "p_lModelTypeId", DbType.Int32, Convert.ToInt32(Model.ModelType));
                db.AddInParameter(dbCommand, "p_bEnabled", DbType.Int32, Convert.ToInt32(Model.IsEnabled));
                db.AddInParameter(dbCommand, "p_lMaxMajor", DbType.Int32, Model.MaxMajorVersion);
                db.AddInParameter(dbCommand, "p_lMaxMinor", DbType.Int32, Model.MaxMinorVersion);
                db.AddInParameter(dbCommand, "p_lWkfId", DbType.Int32, Model.WorkFlow);

                db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Versioning --> SetVersionModel exception[" + ex.Message + "]");
            }
        }

        /// <summary>
        /// Execute P_VERSION_MODEL_Update
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static void UpdateVersionModel(VersioningModel Model)
        {
            try
            {
                //LogManager.Instance().LogDebug("(p_dStartDate<" + StartDate.ToString() + ">)(p_dEndDate<" + EndDate.ToString() + ">)");
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.UpdateVersionModel.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_lRdvmId", DbType.Int32, Model.Identifier);
                db.AddInParameter(dbCommand, "p_lModelTypeId", DbType.Int32, Convert.ToInt32(Model.ModelType));
                db.AddInParameter(dbCommand, "p_bEnabled", DbType.Int32, Convert.ToInt32(Model.IsEnabled));
                db.AddInParameter(dbCommand, "p_lMaxMajor", DbType.Int32, Model.MaxMajorVersion);
                db.AddInParameter(dbCommand, "p_lMaxMinor", DbType.Int32, Model.MaxMinorVersion);
                db.AddInParameter(dbCommand, "p_lWkfId", DbType.Int32, Model.WorkFlow);

                db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Versioning --> UpdateVersionModel exception[" + ex.Message + "]");
            }
        }

        /// <summary>
        /// Execute P_VERSION_MODEL_Get
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <returns></returns>
        public static IDataReader GetVersionModel(long FolderTypeIdentifier)
        {
            try
            {
                LogManager.Instance().LogDebug("(p_lFolderTypeId<" + FolderTypeIdentifier.ToString() + ">)");
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.GetVersionModel.szGetDescription());
                db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int32, FolderTypeIdentifier);

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Versioning --> GetVersionModel exception[" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Execute P_VERSION_CHECKOUT_Get
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <returns></returns>
        public static IDataReader GetCheckOutVersionByFolderType(long FolderTypeIdentifier)
        {
            try
            {
                LogManager.Instance().LogDebug("(p_lFolderTypeId<" + FolderTypeIdentifier.ToString() + ">)");
                var db = DbHelper.DataBase;

                var dbCommand = db.GetNewCommand(DbFunction.GetCheckOutVersion.szGetDescription());
                db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int32, FolderTypeIdentifier);

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Versioning --> GetCheckOutVersionByFolderType exception[" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Execute get P_VERSION_MODEL_HasCheckout
        /// </summary>
        /// <param name="FolderTypeIdentifier"></param>
        /// <returns></returns>
        public static IDataReader HasCheckoutVersionByFolderType(long FolderTypeIdentifier)
        {
            try
            {
                LogManager.Instance().LogDebug("(p_lFolderTypeId<" + FolderTypeIdentifier.ToString() + ">)");
               
                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.HasCheckoutVersion.szGetDescription());
                db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int32, FolderTypeIdentifier);

                return db.ExecuteReader(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Versioning --> HasCheckoutVersionByFolderType exception[" + ex.Message + "]");
                return null;
            }
        }

        /// <summary>
        /// Execute P_DOCUMENT_VERS_DeleteAll to delete all document version.
        /// </summary>
        /// <param name="p_lFolderTypeId">Folder type identifier</param>
        /// <returns></returns>
        public static void DeleteAllDocumentVersionByFolderTypeId(long p_lFolderTypeId)
        {
            try
            {
                LogManager.Instance().LogDebug("DA_Versioning.DeleteAllDocumentVersionByFolderTypeId: (p_lFolderTypeId<" + p_lFolderTypeId.ToString() + ">)");

                var db = DbHelper.DataBase;
                var dbCommand = db.GetNewCommand(DbFunction.DeleteAllDocumentVersion.szGetDescription(), false);
                db.AddInParameter(dbCommand, "p_lFolderTypeId", DbType.Int32, p_lFolderTypeId);

                db.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError("DA_Versioning --> DeleteAllDocumentVersionByFolderTypeId exception [" + ex.Message + "]");
            }
        }
        #endregion
    }
}